package com.example.myproject.util;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/4/16.
 */
public class RestTemplateUtil {
    public static <T> T sendHttp(RestTemplate restTemplate, String url, Object request, Class<T> target, HttpHeaders headers){
        RestTemplate rt = new RestTemplate();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
        HttpEntity<Object> formEntity = new HttpEntity<Object>(request, headers);
        return  rt.postForObject(url,formEntity,target);
    }

    public static <T> T sendHttpByMapParam(RestTemplate restTemplate, String url, Map<String,Object> requestMap, Class<T> target, HttpHeaders headers){
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
        HttpEntity formEntity = new HttpEntity(requestMap, headers);
        return  restTemplate.postForObject(url,formEntity,target);
    }

  /*  public static HttpHeaders getCallAdminConfigCenterHttpHead(ConfigCenterAdminUrlConfig configCenterAdminUrlConfig){
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON);
        requestHeaders.add("X-AUTH-ID",configCenterAdminUrlConfig.getAppId());
        requestHeaders.add("X-AUTH-KEY",configCenterAdminUrlConfig.getAppKey());

        return  requestHeaders;
    }*/


    public static <T> T sendHttpByMapParamByTimeOut(RestTemplate restTemplate, String url, Map<String,Object> requestMap, Class<T> target, HttpHeaders headers){
     /*   MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);*/
        HttpEntity formEntity = new HttpEntity(requestMap, null);
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(5000);// 设置超时
        requestFactory.setReadTimeout(5000);
        restTemplate.setRequestFactory(requestFactory);
        restTemplate.getForEntity(url,String.class);
        return  restTemplate.postForObject(url,formEntity,target);
    }

    /**
     * 第一种方式
     * 第二种方式把服务端代码改为数组接收，在将数组转化为List。
     * @param restTemplate
     * @param url
     * @param target
     * @param headers
     */
    public static List<T> getList(RestTemplate restTemplate, String url, Object request, Class<T> target, HttpHeaders headers){
        HttpEntity<Object> requestEntity = new HttpEntity<Object>(request, headers);
        ResponseEntity<List<T>> result = restTemplate.exchange("https://www.xxx.com",
                        HttpMethod.POST, requestEntity, new ParameterizedTypeReference<List<T>>() {
                        });
        System.out.println("aaa:"+result.getBody());
        return  result.getBody();
    }
}
